package com.cms.admin.service.stuff.impl;

import com.cms.admin.mapper.StuffListMapper;
import com.cms.admin.mapper.StuffSortMapper;
import com.cms.admin.mapper.UserCashAccountIDNoMapper;
import com.cms.admin.mapper.UserListMapper;
import com.cms.admin.model.*;
import com.cms.admin.service.stuff.StuffService;
import com.cms.admin.service.stuff.UserCashAccountService;
import org.apache.commons.lang.math.RandomUtils;
import org.omg.CORBA.PUBLIC_MEMBER;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by YYY-Work on 2017/4/27.
 */
public class StuffServiceImpl implements StuffService{

    private StuffListMapper stuffListMapper;
    private UserListMapper userListMapper;
    private UserCashAccountIDNoMapper userCashAccountMapper;
    private StuffSortMapper stuffSortMapper;

    @Override
    public List<StuffListExt> selectStuffList(PageBean page,StuffListExt example) {

        int count = getRowCount(example);
        page.setAllRow(count);

        example.setOffset(page.getOffset());
        example.setPageSize(page.getPageSize());
        return stuffListMapper.selectByConditionWithLimit(example);
    }

    @Override
    public List<StuffList> selectStuffList(StuffList example) {
        return stuffListMapper.selectByCondition(example);
    }

    @Override
    public int getRowCount(StuffListExt example) {
        return stuffListMapper.countByCondition(example);
    }

    @Override
    public StuffList queryStuffListById(Long stuffid) {
        return stuffListMapper.selectByStuffid(stuffid);
    }

    @Override
    public int updateStuff(StuffListExt record) {
        return stuffListMapper.updateByStuffIdSelective(record);
    }

    @Override
    public int addUser(UserList userList)
    {
        return userListMapper.insertUser(userList);
    }

    @Override
    public  UserList getUserByUserNo(String userNo)
    {
        return userListMapper.getUserByUserNo(userNo);
    }

    @Override
    public int addStuff(StuffListExt record) {
        stuffListMapper.insertSelective(record);
        UserCashAccountIDNo userCashAccountIDNo=new UserCashAccountIDNo();
        userCashAccountIDNo.setOwnerid(record.getStuffid());
        userCashAccountIDNo.setSystemid(0);
        userCashAccountIDNo.setAccountBankName("微信");
        userCashAccountIDNo.setAccountIdno("weixin");
        userCashAccountIDNo.setAccountName("weixin");
        userCashAccountIDNo.setAccountIdentityid("111111111111111111");
        userCashAccountIDNo.setAccounttypeid(1);
        userCashAccountIDNo.setAccountTypeName("微信");
        userCashAccountIDNo.setContactPhone(record.getStuffNo());
        userCashAccountIDNo.setIsUsedFlag((short) 1);
        userCashAccountIDNo.setIsDefaultFlag((short) 0);
        userCashAccountIDNo.setCreateDate(new Date());
        userCashAccountIDNo.setUpdateDate(new Date());
        userCashAccountIDNo.setCreateUserid(record.getUpdateUserid());

        StuffSort stuffSort=new StuffSort();
        stuffSort.setOwnerid(record.getStuffid());
        stuffSort.setComment(record.getStuffName());
        stuffSort.setEvaluatescore(new BigDecimal(5));
        stuffSort.setOrdercount(RandomUtils.nextInt(70)+30);
        userCashAccountMapper.insertSelective(userCashAccountIDNo);

        return stuffSortMapper.insertSelective(stuffSort);
    }

    @Override
    public int addStuff(StuffListExt search, UserList userList) {
        addUser(userList);
        UserList olderUser=getUserByUserNo(userList.getUserNo());
        if (null!=olderUser)
        {
            search.setStuffid(olderUser.getUserid());
            addStuff(search);
        }
        return 0;
    }

    public StuffListMapper getStuffListMapper() {
        return stuffListMapper;
    }

    public void setStuffListMapper(StuffListMapper stuffListMapper) {
        this.stuffListMapper = stuffListMapper;
    }

    public UserListMapper getUserListMapper() {
        return userListMapper;
    }

    public void setUserListMapper(UserListMapper userListMapper) {
        this.userListMapper = userListMapper;
    }

    public UserCashAccountIDNoMapper getUserCashAccountMapper() {
        return userCashAccountMapper;
    }

    public void setUserCashAccountMapper(UserCashAccountIDNoMapper userCashAccountMapper) {
        this.userCashAccountMapper = userCashAccountMapper;
    }

    public StuffSortMapper getStuffSortMapper() {
        return stuffSortMapper;
    }

    public void setStuffSortMapper(StuffSortMapper stuffSortMapper) {
        this.stuffSortMapper = stuffSortMapper;
    }
}
